package org.top10.dealor;

import java.io.UnsupportedEncodingException;

public class ByteBuffer {
	private  byte[]buffer;
	private int cur_Pos=0;
	private final  int Default_SIZE=2048;
	private final int Default_Extend_SIZE=2048;
	public ByteBuffer(int bufSize){
		buffer=new byte[bufSize];
	}
	public ByteBuffer(){
		buffer=new byte[Default_SIZE];
	}
	public void append(byte data[]){
		while(cur_Pos+data.length>=buffer.length){
			expandSize();
		}
		System.arraycopy(data, 0, buffer, cur_Pos, data.length);
		cur_Pos+=data.length;
	}
	
	private void  expandSize(){
		byte bArr[]=new byte[buffer.length+Default_Extend_SIZE];
		int pos=0;
		while(pos!=buffer.length){
			bArr[pos]=buffer[pos++];
		}
		buffer=bArr;
	}
	public void append(byte b){
		if(cur_Pos+1>=buffer.length){
			expandSize();
		}
		buffer[cur_Pos++]=b;
	}
	public String toString(){
		return new String(buffer,0,cur_Pos);
	}
	public String toString(String encoding) throws UnsupportedEncodingException{
		return new String(buffer,0,cur_Pos,encoding);
	}
	                           
}
